package com.yanqu.road.server.cmd.union.cross.honors;

import com.yanqu.road.entity.rank.UnionValueRank;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.cross.CrossGroupUnionValueRankListModel;
import com.yanqu.road.logic.pb.CrossUnionPb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsRankMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_UNION_HONORS_RANK_LIST, desc = "榜单")
public class CrossGetUnionHonorsRankListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();

        int groupId = ServerGroupMgr.getGroupIdByServerId(serverId);

        RankProto.UnionRankRespMsg.Builder respMsg = RankProto.UnionRankRespMsg.newBuilder();
        respMsg.setMyRank(-1);

        RankProto.UnionRankListMsg.Builder unionListMsg = RankProto.UnionRankListMsg.newBuilder();

        CrossGroupUnionValueRankListModel model = CrossUnionHonorsRankMgr.getCrossGroupUnionRankListModel(groupId);
        if(model != null){
            List<UnionValueRank> rankList = new ArrayList<>(model.getRankList());
            for (int i = 0; i < rankList.size(); i++) {
                int rank = i+1;
                UnionValueRank unionRank = rankList.get(i);
                RankProto.UnionRankTempMsg.Builder rankTemp = RankProto.UnionRankTempMsg.newBuilder();

                rankTemp.setUnionUid(unionRank.getUnionUid());
                UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionRank.getUnionBaseInfo());
                rankTemp.setUnionBaseData(unionMsg);
                rankTemp.setValue(unionRank.getValue().toString());
                CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionRank.getUnionUid());
                if (null != unionInfo) {
                    List<UnionProto.UnionMemberRankGradeTemp> memberRankGradeMsg = CrossUnionMgr.getMemberRankGradeMsg(unionInfo);
                    rankTemp.addAllRankGrade(memberRankGradeMsg);

                    for (Map.Entry<Integer, Integer> gradeEntry : unionInfo.getGradeAmountMap().entrySet()) {
                        rankTemp.addOriginalGradeAmount(CrossUnionPb.parseUnionRankGradeJoinLimitTemp(gradeEntry.getKey(), gradeEntry.getValue()));
                    }
                }
                unionListMsg.addRankList(rankTemp);

                if(context.getRet() == 0 && context.getUnionInfo().getUnionUid().equals(unionRank.getUnionUid())) {
                    respMsg.setMyRank(rank);
                }
            }
        }
        respMsg.setRankList(unionListMsg);

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_HONORS_RANK_LIST, respMsg));
    }

    @Override
    public void sendData() {

    }
}
